﻿using UnityEngine;
using System.Collections;

//using Antares.QRCode;

public class WebManager : MonoBehaviour
{
	
	protected Texture2D m_uploadImage;
	protected Texture2D m_downloadTexture;
	string m_info = "";
	
	protected Texture2D qrCodeImage;
	private string _textToEncode = "Hello QRCode!";
	private int _width = 256;
	private int _height = 256;
	
	private string _decodedString = "";
	public GameObject target;
	private RenderTexture txt;
	// Use this for initialization
	void Start ()
	{

	}
	
	// Update is called once per frame
	void Update ()
	{
		if (Input.GetKeyDown (KeyCode.Mouse0)) {
			InitCamera ();
		}
		
		
	}

 
	
	IEnumerator GetCapture ()
	{
		
		yield return new WaitForEndOfFrame ();
		
		int width = Screen.width;
		
		int height = Screen.height;
		
		Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
		
		tex.ReadPixels (new Rect (0, 0, width, height), 0, 0, true);
		
		byte[] imagebytes = tex.EncodeToPNG ();//转化为png图
		
		tex.Compress (false);//对屏幕缓存进行压缩
		
//		image.mainTexture = tex;//对屏幕缓存进行显示（缩略图）
		
//		File.WriteAllBytes (Application.dataPath + "/screencapture.png", imagebytes);//存储png图
		
	}

	
	void InitCamera ()
	{
		target.transform.localPosition = new Vector3 (0, 0, -2);
	}

	IEnumerator IRequestPNG ()
	{  
		
		yield return new WaitForEndOfFrame ();
		
		int width = Screen.width;
		
		int height = Screen.height;
		
		Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
		
		tex.ReadPixels (new Rect (0, 0, width, height), 0, 0, true);
		
		byte[] imagebytes = tex.EncodeToPNG ();//转化为png图
		
//		tex.Compress (true);//对屏幕缓存进行压缩
	
//		Texture2D myTexture2D = new Texture2D (txt.width, txt.height);
//		RenderTexture.active = txt;
//		myTexture2D.ReadPixels (new Rect (0, 0, txt.width, txt.height), 0, 0);
//		myTexture2D.Apply ();
//		RenderTexture.active = null;
		
		
		m_uploadImage = tex;
		
		// 将图片的字节读入byte数组  
		byte[] bs = m_uploadImage.EncodeToPNG ();  
		// 创建WWWForm  
		WWWForm form = new WWWForm ();  
		// 添加二进制的Post数据  
		form.AddBinaryData ("picture", bs, "screenshot", "image/png");  
		// 向服务器提交二进制的Post数据  
		WWW www = new WWW ("http://www.iqcoo.cc/Test.php", form);
		// 等待服务器响应  
		yield return www;  
		
		if (www.error != null) {  
			m_info = www.error;  
			yield return null;  
		}
		
		target.transform.localPosition = new Vector3 (0, 0, 20);
		
		Debug.Log ("Info:" + m_info);
		// 收到服务器返回的图片数据  
//		m_downloadTexture = www.texture;  
		_textToEncode = System.Text.Encoding.Default.GetString (www.bytes);
		Debug.Log (System.Text.Encoding.Default.GetString (www.bytes));
//		qrCodeImage = QRCodeProcessor.Encode (_textToEncode, _width, _width, ErrorCorrectionLevel.H, "Dude.. :D");
		target.GetComponent<MeshRenderer> ().material.mainTexture = qrCodeImage;
	}

	void OnGUI ()
	{
//		if (m_downloadTexture != null)  
//			GUI.DrawTexture (new Rect (0, 0, m_downloadTexture.width,  
//			                         m_downloadTexture.height),   
//			                m_downloadTexture);  
		
//		if (GUI.Button (new Rect (0, 0, 150, 100), "Take photo")) {  
//			TakePhoto ();
//		} 
	}

	void TakePhoto ()
	{
		
		StartCoroutine (IRequestPNG ());  
		this.gameObject.GetComponent<AudioSource> ().Play ();
	}
}
